<?php

/**
 * Created by PhpStorm.
 * User: 孙克强
 * Date: 2018/1/22
 * Time: 15:56
 */
class Role extends CI_Controller
{
    //主页
    public function index(){$this->load->view('role/allRoles');}
    //添加用户
    public function addRole(){$this->load->view('role/addRole');}
    //修改用户
    public function updateRole(){$this->load->view('role/updateRole');}
    //数据传输
    public $token_data;

    /**
     * Role constructor.
     */
    public function __construct(){
        parent::__construct();
        $this->load->model('role_model');
        $userToken = new Usertoken_helper();
        $this->token_data['token'] = $userToken->check_token($this);
        //$this->token_data['user_token'] = $userToken->check_userToken($this);
        $this->token_data['user_token']['flag']=1;
    }

    /**
     * 添加接口
     */
    public function imp_add(){
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
                $data = $_POST['data'];
                $arr = json_decode($data, true);
                $this->role_model->addRole($arr);
                $this->token_data['success'] = 1;
            }
            $json = json_encode($this->token_data);
            echo $json;
        }else {
            echo '0';
        }
    }

    /**
     * roleList接口
     */
    public function imp_index(){
        if($this->token_data['user_token']['flag']) {
            //如果访问合法便进行数据操纵
            if ($this->token_data['token']['tag']) {
                $data = $this->role_model->getAll();
                $this->token_data['data'] = $data;
            }
            $json = json_encode($this->token_data['data']);
            echo $json;
        }else {
            echo '0';
        }
    }

    /**
     * 删除接口
     */
    public function imp_del(){
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
                $role_id = $_POST['role_id'];
                $this->role_model->deleteRole($role_id);
                $this->token_data['success'] = 1;
            }
            $json = json_encode($this->token_data);
            echo $json;
        }else {
            echo '0';
        }
    }

    /**
     * 修改接口
     */
    public function imp_update(){
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
                $data = $_POST['data'];
                $arr = json_decode($data, true);
                $this->role_model->updateRole($arr);
                $this->token_data['success'] = 1;
            }
            $json = json_encode($this->token_data);
            echo $json;
        }else {
            echo '0';
        }
    }

    /**
     * 回显接口
     */
    public function imp_edit(){
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
                $role_id = $_POST['role_id'];
                $this->token_data['role'] = $this->role_model->getRoleById($role_id);
                $this->token_data['success'] = 1;
            }
            $json = json_encode($this->token_data);
            echo $json;
        }else {
            echo '0';
        }
    }

    /**
     * 模糊查询接口
     */
    public function search(){
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
                $role_search = $_POST['role_search'];
                $this->token_data['data'] = $this->role_model->getSearch($role_search);
                $this->token_data['success'] = 1;
            }
            $json = json_encode($this->token_data);
            echo $json;
        }else {
            echo '0';
        }
    }

}